package cn.edu.zjicm.wordsnet_d.activity;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.ProgressBar;
import android.widget.TextView;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.fragment.ExamSpellFragment;

import com.umeng.analytics.MobclickAgent;

public class ExamSpellActivity extends WordBaseActivity implements Runnable {
	public static void startActivity(Context context){
		Intent i = new Intent(context, ExamSpellActivity.class);
		context.startActivity(i);
//		finish();
	}

	public TextView testResultText1Front;
	public TextView testResultText1;
	public ProgressBar mProgressBar1;

	WordFactory wf = WordFactory.getInstance(this);
		
	private List<OneWord> wordsList;
	private OneWord curWord;
		
	Handler handler = new Handler();	
	
	ExamSpellFragment mSpellFragment = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//设定调整音量为媒体音量
		setVolumeControlStream(AudioManager.STREAM_MUSIC); 	
		initTitle();	
		
		setContentView(R.layout.activity_exam_spell);
				
		testResultText1Front = (TextView) findViewById(R.id.test_result_text1_front);
		testResultText1 = (TextView) findViewById(R.id.test_result_text1);
		mProgressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
			
		startTest();
		initTestCount();
		displayResult();
	}
	
	void initTitle(){
		setTitleBarStyle(WordBaseActivity.TITLE_BAR_STYLE_BACK_BUTTON);
		setTitleText("拼写");		
		/*setRightText("结束", new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				stopTest();
			}
		});*/
	}			
	
	void startTest(){	
		//wordsList = wf.getLastForgetWords();	
		wordsList = wf.getNewWordOrLastForgetWords();	
		//if(wordsList.size() > 20)
		//	wordsList = wordsList.subList(0, 20);
		
		nextQuestion();
	}

	private void nextQuestion() {
		// TODO Auto-generated method stub
		handler.removeCallbacks(this);
		
		curWord = nextWord(); 
		if (curWord != null) {
			updateResult();
			mode1();
		}else {
			stopTest();
		}
	}
	
	OneWord nextWord(){
		if (wordsList.size()>0)			
			return wordsList.get(0);
		else
			return null;
	}
		
	public void next() {
		// TODO Auto-generated method stub
		wordsList.remove(0);
		handler.postDelayed(this, TEST_INTERVAL);	
	}
	
	//结束测试，进入TestEndActivity
	private void stopTest(){				
		finish();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean consumed = false;
		switch (keyCode) {
			case KeyEvent.KEYCODE_BACK:
				stopTest();
		}
		return consumed;
	}
				
	@Override
	public void run() {
		// TODO Auto-generated method stub
		this.nextQuestion();
	}
	
	public static final int TEST_INTERVAL = 0;
	
	void mode1(){			
		if(mSpellFragment == null){
			mSpellFragment =  new ExamSpellFragment(curWord, this);
			FragmentTransaction ft 	= getSupportFragmentManager().beginTransaction();
			ft.add(R.id.word_exam_spell_fragment_container, mSpellFragment);
			ft.commit();
		}else
			mSpellFragment.initViewForNewWord(curWord);

	}		

	int newWordNumber, newWordCount;
	
	void initTestCount(){
		newWordNumber = wordsList.size();
		newWordCount = 1;
	}
	
	void displayResult(){
		testResultText1Front.setText("拼写");
		testResultText1.setText(newWordCount + "/" +newWordNumber );

		if(newWordNumber==0) 
			mProgressBar1.setProgress(100);
		else 
			mProgressBar1.setProgress(100 * newWordCount / newWordNumber);		
	}		
	
	public void updateResult(){
		newWordCount++;
		displayResult();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
}

